<template>
  <div id="home">
    <!-- 顶部导航栏 -->
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <!-- 固定顶部的tab control -->
    <tab-control :titles="tabControlTitles"
                  @tabClick="tabControlClick" 
                  ref="tabControlFixed"
                  class="top-fixed"
                  v-show="isTabFixed"/>
    <!-- 页面内容 -->
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            :pull-up-load="true"
            @scrollProbe="contentScroll"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @imgLoad="swiperImgLoad"/>
      <recommend-view :recommends="recommends" />
      <tab-control :titles="tabControlTitles"
                   @tabClick="tabControlClick" 
                   ref="tabControl"/>
      <goods-list :goods="showGoods" />
    </scroll>
    <!-- 回到顶部 -->
    <back-top @click.native="backTopClick"
              v-show="isShowBackTop" />
  </div>
</template>

<script>
  import HomeSwiper from './childComps/HomeSwiper'
  import RecommendView from './childComps/RecommendView'

  import NavBar from "components/common/navbar/NavBar"
  import Scroll from 'components/common/scroll/Scroll'
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'
  import BackTop from 'components/content/backTop/BackTop'

  import { getHomeMultidata, getHomeGoods } from 'network/home'
  import { debounce } from 'common/utils'
  import { itemListenerMixin,backTopMixin } from 'common/mixin'

  export default {
    name: "Home",
    components: {
      HomeSwiper,
      RecommendView,
      NavBar,
      Scroll,
      TabControl,
      GoodsList,
      BackTop
    },
    mixins: [itemListenerMixin,backTopMixin],
    data() {
      return {
        banners: [],
        recommends: [],
        tabControlTitles: ['流行', '新款', '精选'],
        goods: {
          'pop': { 'page': 0, 'list': [] },
          'new': { 'page': 0, 'list': [] },
          'sell': { 'page': 0, 'list': [] },
        },
        currentType: 'pop',
        isTabFixed: false,
        tabOffsetTop: 0,
        saveY: 0
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    created() {
      // 1.请求多个数据
      this.getHomeMultidata()

      // 2.请求商品数据
      this.getHomeGoodsData('pop')
      this.getHomeGoodsData('new')
      this.getHomeGoodsData('sell')
    },
    activated() {
      this.$refs.scroll.refresh()
      // 恢复原来浏览高度（如有）
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
    },
    deactivated() {
      // 保存首页浏览的高度位置
      this.saveY = this.$refs.scroll.getScrollY()
      // 暂停监听图片加载
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    },
    methods: {
      /* 
      点击事件监听方法
      */
      tabControlClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop';
            break
          case 1:
            this.currentType = 'new';
            break
          case 2:
            this.currentType = 'sell';
            break
        }

        this.$refs.tabControl.currentIndex = index
        this.$refs.tabControlFixed.currentIndex = index
      },
      contentScroll(position) {
        // 当屏幕滚动到一定位置后，显示回到顶部按钮
        this.isShowBackTop = -position.y > 1000

        // 显示tabControlFixed
        this.isTabFixed = -position.y >= this.tabOffsetTop

      },
      loadMore() {
        this.getHomeGoodsData(this.currentType)
        this.$refs.scroll.finishPullUp()
      },
      swiperImgLoad() {
        // 监听轮播图是否已加载图片
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop;
      },

      /* 
      网络请求相关方法
      */
      getHomeMultidata() {
        // 请求banner、推荐分类数据
        getHomeMultidata().then(res => {
          // console.log(res)
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoodsData(type) {
        // 请求商品数据
        const page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          // console.log(res)
          // Array.push(...[Arr])：直接把[Arr]数组内容逐一添加到[Array]数组中。
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1
        })
      }
    }
  }
</script>

<style scoped>
  #home {
    /* position: relative; */
    height: 100vh;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #ffffff;

    /* 当使用浏览器原生滚动方法时，才需要 */
    /* position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 9; */
  }

  .content {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .top-fixed {
    position: fixed;
    top: 44px;
    left: 0;
    right: 0;
  }
</style>
